package com.weiyi.mashangzhu.Landlord.Adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by win7 on 2017/10/27.
 */

public class GridAdapter_Id extends ArrayAdapter<String> {
    private Context mContext;
    private int layoutResourceId;
    private List<String> mGridData = new ArrayList<String>();

    public GridAdapter_Id(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.layoutResourceId = resource;
        this.mGridData = objects;
    }

    public void setGridData(ArrayList<String> mGridData) {
        this.mGridData = mGridData;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.close = (ImageView) convertView.findViewById(R.id.close);
            holder.add= (RelativeLayout) convertView.findViewById(R.id.add_3);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 加载本地图片
        if (mGridData.size()<=3){
            if (position==mGridData.size()+1){
                holder.add.setVisibility(View.VISIBLE);
                holder.img.setVisibility(View.GONE);
                holder.close.setVisibility(View.GONE);
            }else {
                holder.add.setVisibility(View.GONE);
                holder.img.setVisibility(View.VISIBLE);
                holder.close.setVisibility(View.VISIBLE);
                File file = new File(mGridData.get(position));
                Glide.with(mContext).load(file).into(holder.img);
                holder.close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApp.images_id.remove(position);
                        mGridData.remove(position);
                        notifyDataSetChanged();
                    }
                });
            }
        }else {
            if (position==mGridData.size()+1){
                holder.add.setVisibility(View.GONE);
                holder.img.setVisibility(View.GONE);
                holder.close.setVisibility(View.GONE);
            }else {
                holder.add.setVisibility(View.GONE);
                holder.img.setVisibility(View.VISIBLE);
                holder.close.setVisibility(View.VISIBLE);
                File file = new File(mGridData.get(position));
                Glide.with(mContext).load(file).into(holder.img);
                holder.close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApp.images_id.remove(position);
                        mGridData.remove(position);
                        notifyDataSetChanged();
                    }
                });
            }
        }


        return convertView;
    }

    private class ViewHolder {
        ImageView img,close;
        RelativeLayout add;

    }


}
